/***************************************************
  File: src/control.c
  Description: Audio Cup Generic Control ( object )
  Author: Brant Anderson
  Date: 10/25/01
  Last Update: 10/29/01
  Status: Working
  To Do:
 ***************************************************/



#include <stdio.h>
#include <string.h>
#include "../include/control.h"
char songpath[192];
/* Tells control what state the player is in */
static State PlayerState;

static XA_Control *xaPlayer = NULL;
/* OV_Control *ovPlayer = NULL; */


int getCount( char *playlist ) {
/** Determines how many songs are in the list **/

    int x;
    int i = 0;

    FILE *fpList;
    fpList = fopen( playlist, "r" );
    if( fpList != 0 ) {
        while( !feof(fpList) ) {
            x = getc( fpList );
            if( x == '\n' ) i++;
        }   
    }
    fclose( fpList );
printf( "%i", i - 1 );
	return i - 1;
}


char *getSong( char *playlist, int songnum ) {
/** Opens a playlist, and returns the path to the song specified **/

    int i = 0;
    int x = 0;
    int y = 0;
    
    FILE *fpList;
    fpList = fopen( playlist, "r" );
    if( fpList != 0 ) {
        while( !feof(fpList) && i <= songnum ) {
            x = getc( fpList );
            if( x != '\n' ) {
                songpath[y] = x;
                y++;
            } else if( x == '\n' && i == songnum ) {
                songpath[y] = '\0';
                i++;
            } else {
                for( y = 0; y < 192; y++ ) {
                    songpath[y] = (int)NULL;
                }
                y = 0;
                i++;
            }
        }   
    }
    fclose( fpList );

	return songpath;
}

SongInfo getSongInfo( SongInfo songinf ) {
    int i = 0;
    int j = 0;
    int k = 0;
    int element = 0;
    
    char elif[265];
    char tmp[64];
    char test[3];
    
    songinf.scroller = 0;
    while( songinf.filename[i] != '\0' ) {    
        i++;
    }
    i = i - 5;
    while( songinf.filename[i] != '/' ) {
        elif[j] = songinf.filename[i];
        i--;
        j++;
    }
    elif[j] = '\0';
    j--;
    i = 0;
    while( j >= 0 ) {
        test[0] = elif[j - 2];
        test[1] = elif[j - 1];
        test[2] = elif[j];
        
        tmp[i] = elif[j];
        
        if( element == 0 && test[0] == ' ' && test[1] == '-' && test[2] == ' ' ) {
            tmp[i] = '\0';
            element++;
            sprintf( songinf.artist, "%s", tmp );
            printf( "\n\nArtist: %s\n", songinf.artist );
            for( k = 0; k < 64; k++ ) {
                tmp[k] = (int)NULL;
            }
            j = j - 2;
            i = -1;
        } else if( element == 1 && test[0] == ' ' && test[1] == '-' && test[2] == ' ' ) {
            tmp[i] = '\0';
            element++;
            sprintf( songinf.album, "%s", tmp );
            printf( "Album: %s\n", songinf.album );
            for( k = 0; k < 64; k++ ) {
                tmp[k] = (int)NULL;
            }
            j = j - 2;
            i = -1;
        } else if( element == 2 && test[0] == ' ' && test[1] == '-' && test[2] == ' ' ) {
            tmp[i] = '\0';
            element++;
            sprintf( songinf.track, "%s", tmp );
            printf( "Track: %s\n", songinf.track );
            for( k = 0; k < 64; k++ ) {
                tmp[k] = (int)NULL;
            }
            j = j - 2;
            i = -1;
        }
        i++;
        j--;
    }
    tmp[i] = '\0';
    sprintf( songinf.title, "%s", tmp );
    printf( "Title: %s\n\n", songinf.title );
    
    songinf.ctype = openSong( songinf );
    songinf.loop = 1;
    
	return songinf;
}

/** Opens a song to play, and returns the control type ( MP3/OGG ) **/
int openSong( SongInfo songinf ) {

	char ext[3];
	int ctype = 0;

	/** Make sure the next song starts cleanly, I don't care if these fail. **/
	if( PlayerState == PLAYER_PLAY || PlayerState == PLAYER_PAUSE ) {

		/* In the next bit of code, a new player will be created anyway */
		exitXA( xaPlayer ); /* if fail, who cares... */
		/* exitOV( ovPlayer );  if fail, who cares... */
		PlayerState = PLAYER_NONE;

	}

	/** Break filename into parts **/
	/**** Temperary until filename breakage function is written ****/
	sprintf( ext, "mp3" );

	if( ext[0] == 'm' && ext[1] == 'p' && ext[2] == '3' ) {

		if( PlayerState == PLAYER_NONE || PlayerState == PLAYER_ERROR ) {

			if( (xaPlayer = getXAPlayer( )) == NULL ) {

				PlayerState = PLAYER_ERROR;
				printf( "getXAPlayer: Couldn't get player.\n" );
				ctype = -1;

			} else {

				PlayerState = PLAYER_OPEN;
				openXA( xaPlayer, songinf.filename );
				ctype = MP3;

			}

		} else {

			PlayerState = PLAYER_OPEN;
			openXA( xaPlayer, songinf.filename );
			ctype = MP3;

		}

	} /* else if( ext[0] == 'o' && ext[1] == 'g' && ext[2] == 'g' ) { ... } */
	return ctype;
/**
 -1: Error: player doesn't exist
MP3: Success
OGG: Success
 **/
}



/** Plays a song, assuming the player has been created **/
int playSong( int ctype ) {

	if( ctype == -1 ) {
		PlayerState = PLAYER_ERROR;
		printf("playSong:Error -3: ctype is -1: Try getPlayer() again.\n");
		return -3;
	} else if( ctype == 0 ) {
		PlayerState = PLAYER_NONE;
		printf("playSong:Error -1: Audio player doesn't exist. Try openSong() again.\n");
		return -1;
	} else if( ctype == MP3 ) {
		if( xaPlayer ) playXA( xaPlayer );
		PlayerState = PLAYER_PLAY;
		return 1;
	} /* else if( ctype == OGG ) {
		if( ovPlayer ) playOV( ovPlayer );
		PlayerState = PLAYER_PLAY;
		return 1;
	} */ else {
		PlayerState = PLAYER_ERROR;
		printf("playSong:Error -2: ctype is %d: Audio player doesn't exist.\n* This logically will never happen.\n", ctype);
		return -2;
	}
/**
-1: Error: player doesn't exist
-2: Error: player doesn't exist and ctype is an unknown value
-3: Error: error in getPlayer(), try getPlayer() again
 1: Success
 **/
}

int pauseSong( int ctype ) {

	if( ctype == -1 ) {
		PlayerState = PLAYER_ERROR;
		printf("pauseSong:Error -3: ctype is -1: Try getPlayer() again.\n");
		return -3;
	} else if( ctype == 0 ) {
		PlayerState = PLAYER_NONE;
		printf("pauseSong:Error -1: Audio player doesn't exist.\n");
		return -1;
	} else if( ctype == MP3 ) {
		if( xaPlayer ) pauseXA( xaPlayer );
		PlayerState = PLAYER_PAUSE;
		return 1;
	} /* else if( ctype == OGG ) {
		if( ovPlayer ) pauseOV( ovPlayer );
		PlayerState = PLAYER_PAUSE;
		return 1;
	} */ else {
		PlayerState = PLAYER_ERROR;
		printf("pauseSong:Error -2: ctype is %d: Audio player doesn't exist.\n* This logically will never happen.\n", ctype);
		return -2;
	}
/**
-1: Error: player doesn't exist
-2: Error: player doesn't exist and ctype is an unknown value
-3: Error: error in getPlayer(), try getPlayer() again
 1: Success
 **/

}

int stopSong( int ctype ) {

	if( ctype == -1 ) {
		PlayerState = PLAYER_ERROR;
		printf("stopSong:Error -3: ctype is -1: Try getPlayer() again.\n");
		return -3;
	} else if( ctype == 0 ) {
		PlayerState = PLAYER_NONE;
		printf("stopSong:Error -1: Audio player doesn't exist.\n");
		return -1;
	} else if( ctype == MP3 ) {
		if( xaPlayer ) stopXA( xaPlayer );
		PlayerState = PLAYER_STOP;
		return 1;
	} /* else if( ctype == OGG ) {
		if( ovPlayer ) stopOV( ovPlayer );
		PlayerState = PLAYER_STOP;
		return 1;
	} */ else {
		PlayerState = PLAYER_ERROR;
		printf("stopSong:Error -2: ctype is %d: Audio player doesn't exist.\n* This logically will never happen.\n", ctype);
		return -2;
	}
/**
-1: Error: player doesn't exist
-2: Error: player doesn't exist and ctype is an unknown value
-3: Error: error in getPlayer(), try getPlayer() again
 1: Success
 **/

}

int seekSong( int ctype, unsigned short offset ) {

	if( ctype == -1 ) {
		PlayerState = PLAYER_ERROR;
		printf("seekSong:Error -3: ctype is -1: Try getPlayer() again.\n");
		return -3;
	} else if( ctype == 0 ) {
		PlayerState = PLAYER_NONE;
		printf("seekSong:Error -1: Audio player doesn't exist.\n");
		return -1;
	} else if( ctype == MP3 ) {
		if( xaPlayer ) seekXA( xaPlayer, offset );
		PlayerState = PLAYER_PLAY;
		return 1;
	} /* else if( ctype == OGG ) {
		if( ovPlayer ) seekOV( ovPlayer );
		PlayerState = PLAYER_PLAY;
		return 1;
	} */ else {
		PlayerState = PLAYER_ERROR;
		printf("seekSong:Error -2: ctype is %d: Audio player doesn't exist.\n* This logically will never happen.\n", ctype);
		return -2;
	}
/**
-1: Error: player doesn't exist
-2: Error: player doesn't exist and ctype is an unknown value
-3: Error: error in getPlayer(), try getPlayer() again
 1: Success
 **/

}

int closePlayer( int ctype ) {

	if( ctype == MP3 ) {
		if( xaPlayer ) exitXA( xaPlayer );
		ctype = 0;
	} /* else if( ctype == OGG ) {
		if( ovPlayer ) exitOV( ovPlayer );
		ctype = 0;
	} */

	if( ctype == -1 ) {
		PlayerState = PLAYER_ERROR;
		printf("playSong:Error -3: ctype is -1: Player never existed.\n");
		return -3;
	} else if( ctype == 0 ) {
		PlayerState = PLAYER_NONE;
		return 1;
	} else {
		PlayerState = PLAYER_ERROR;
		printf("playSong:Error -4: ctype is %d: Try getPlayer() again.\n* This logically will never happen.\n", ctype);
		return -4;
	}
/**
-1: Error: player never existed
-3: Error: error in getPlayer(), try getPlayer() again
-4: Error: ctype unknown player error, try getPlayer() again
 1: Success
 **/

}

int getMessage( int ctype, XA_Message *xaMessage ) {

	int status;

		if( ctype == MP3 ) {
			if( xaPlayer ) status = getXAMessage( xaPlayer, xaMessage );
		} /*** OGG GOES HERE ***/
		else status = -22;

	return status;
}
